package sparrowGui.components
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	
	import sparrowGui.i.IDispatcherEx;
	import sparrowGui.i.IMgrUICompent;
	import sparrowGui.uiStyle.UIStyleMgr;
	
	/**
	 * 基本模块,基本UI适配器
	 * 主要设计思路是初始的时间将皮肤放入,然后对皮肤显示对象增加控制的事件的操作.
	 * 就将插件一样,想给皮肤什么功能就用插件直接插上去.
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class BaseUIComponent extends RichSprite implements IDispatcherEx,IMgrUICompent
	{
		/**
		 * 本组件控制的皮肤
		 */
		protected var _skin:DisplayObject;
		private var _enabled:Boolean = true;
//		private var _name:String;
		
		public function BaseUIComponent(uiVars:Object=null)
		{
			super();
			setUI(uiVars);
			setStyleByDefault();
//			addListener();
		}
		
		/**
		 * 通过皮肤变量构造组件
		 * 如果输入的皮肤的parent皮肤为空的话会把皮肤addChild到本组件里面，反之不会addChild
		 * @param uiVars	支持三种类型(如果是DisplayObject就直接设为皮肤,字符串就会查皮肤表，空会生成默认皮肤)
		 */
		protected function setUI(uiVars:Object=null):void
		{
			if(uiVars is DisplayObject)
				_skin = uiVars as DisplayObject;
			else if(uiVars is String)
				_skin = UIStyleMgr.getIns().createSkinUI(String(uiVars));
			else
				_skin = UIStyleMgr.getIns().createSkinUI(getDefaultUIName());
/*			else if(uiVars is String)
				_skin = SparrowGUI.getIns().getDefaultUIByName(String(uiVars));
			else
				_skin = SparrowGUI.getIns().getDefaultUIByName(getDefaultSkinName());*/
			
//			else
//				throw new Error(toString()+"设置属性有异常");
//			else
//				_skin = SparrowGUI.getIns().getUIByVars(uiVars);
			
			if(!_skin)
				throw new Error(toString()+","+getDefaultUIName()+",默认皮肤有异常");
			
			if(!_skin.parent)
				embeSkin = true;
			
			if(_skin)
				name = _skin.name;
		}
		
		public function setStyleByDefault():void
		{
//			SparrowGUI.getIns().setComponentDefaultStyle(this);
		}
		
		
		/**
		 * 改变样式
		 * 单只改变皮肤,其它监听不变
		 * (要移除原先的皮肤监听和清对象,不然会回收不了)
		 * @param uiVars 可以是动态对象，也可以是SkinVars()不能为空
		 */
		public function changeUI(uiVars:Object):void
		{
			removeSkinListen();
			uiVars = uiVars.isGUIVars?uiVars.vars:uiVars;
			setUI(uiVars);
			setStyleByDefault();
		}
		
		/**
		 * 单只改变皮肤,其它监听不变
		 * (要移除原先的皮肤监听和清对象,不然会回收不了)
		 * @param value
		 
		public function changeSkinStyle(uiVars:Object=null):void
		{
			setUI(uiVars);
		}*/

		/**
		 * 单只改变皮肤,其它监听不变
		 * (要移除原先的皮肤监听和清对象,不然会回收不了)
		 * @param value
		 
		public function changeSkin(value:Sprite):void
		{
			removeListener();
			_skin = value;
		}*/
		
		/**
		 * 通过默认包设置属性
		 * @param vars
		 
		public function setAttrByVars(vars:Object):void
		{
			if(vars.isGUIVars)
				SparrowUtil.copyDynamicCopy(this,vars.vars);
			else
				SparrowUtil.copyDynamicCopy(this,vars);
		}*/
		
		protected function draw():void
		{
			
		}
		
		/**
		 * 增加监听,关联皮肤相关引用。
		 */
		protected function addSkinListen():void
		{
			
		}
		
		/**
		 * 移出监听,断开皮肤相关引用。
		 */
		protected function removeSkinListen():void
		{
			
		}
		
		//// 显示对象操作 //////////////////
		
		
		public function get skin():DisplayObject
		{
			return _skin;
		}
		
		public function addSkinToParent(parent:DisplayObjectContainer):void
		{
			parent.addChild(skin);
		}
		
		public function removeSkinFromParent():void
		{
			if(skin.parent)
				skin.parent.removeChild(skin);
		}
		
		/**
		 * 获取默认皮肤名字，用于通过UI管理查找默认样式
		 * @return 
		 */
		public function getDefaultUIName():String
		{
			return "component";
		}
		
		override public function set x(value:Number):void
		{
			if(embeSkin)
				super.x = value;
			else
				_skin.x = value;
		}
		
		override public function get x():Number
		{
			if(embeSkin)
				return super.x;
			else
				return _skin.x;
		}
		
		override public function set y(value:Number):void
		{
			if(embeSkin)
				super.y = value;
			else
				_skin.y = value;
		}
		
		override public function get y():Number
		{
			if(embeSkin)
				return super.y;
			else
				return _skin.y;
		}
		
		override public function set visible(value:Boolean):void
		{
			if(embeSkin)
				super.visible = value;
			else
				_skin.visible = value;
		}
		
		override public function get visible():Boolean
		{
			if(embeSkin)
				return super.visible;
			else
				return _skin.visible;
		}

		/**
		 * 名称
		 
		override public function get name():String
		{
			return _name;
		}*/

		/**
		 * @private
		 */
		override public function set name(value:String):void
		{
			super.name = value;
			try
			{
				_skin.name = value;
			} 
			catch(error:Error) 
			{
				// 时间轴上的对象是不能修改名称的，所以用了个笨方法…
			}
		}
		
		override public function dispose():void
		{
			removeSkinListen();
			removeAllListeners();
//			removeFromParent();
			_skin = null;
		}

		/**
		 * 是否可用
		 */
		public function get enabled():Boolean
		{
			return _enabled;
		}

		/**
		 * @private
		 */
		public function set enabled(value:Boolean):void
		{
			_enabled = value;
			if(skin is InteractiveObject)
				(skin as InteractiveObject).mouseEnabled = value;
		}
		
		/**
		 * 设置皮肤嵌入本组件
		 * @param value
		 */
		public function set embeSkin(value:Boolean):void
		{
			if(value)
				this.addChild(_skin);
			else
				if(_skin.parent==this)
					this.removeChild(_skin);
		}
		
		/**
		 * 是否把皮肤嵌入到组件里面(即是否把皮肤addChild到自己)
		 * @return 
		 */
		public function get embeSkin():Boolean
		{
			return (skin && skin.parent && skin.parent == this);
		}
	}
}